package com.huifaapp.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.ShopYouhuiShareActivity;
import com.huifaapp.mall.activity.Specialactivity;
import com.huifaapp.mall.activity.Tmall_shopkeeperFragment;
import com.huifaapp.mall.activity.TodaysSelectionFragment;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.bean.Tmall_shopkeeper_hostGoodsBean;
import com.huifaapp.mall.uitils.Contig;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.data;

/**
 * Created by duanyu on 2017/12/23.
 */

public class ShopkeeperAdapter extends  XRecyclerView.Adapter<ShopkeeperAdapter.ShopkeeperViewHolder>implements View.OnClickListener {
    private  List<Tmall_shopkeeper_hostGoodsBean.DataBean.ListBean>goodsBeanlist;
    private Context context;
    ArrayList<String> arrayList;
//    private List<Boolean> isChecked;    //每个条目是否为选中
//    private List<String> numbers;       //每个条目应显示的数字
//    private List<Integer> checkedIndex;   //保存选中的下标
//    private List<CheckBox> checkBox;      //保存选中的checkBox
//    private int maxChecked = 9;       //最多选中几个
//    private int checkedNumber = 0;    //选中的个数
    //        private int length = list.size();  //条目的总长
    private List<Boolean> isChecked;    //每个条目是否为选中
    private List<String> numbers;       //每个条目应显示的数字
    private List<Integer> checkedIndex;   //保存选中的下标
    private List<CheckBox> checkBox;      //保存选中的checkBox
    private int maxChecked = 9;       //最多选中几个
    private int checkedNumber = 0;    //选中的个数

    private int length = 1000000;  //条目的总长
    public ShopkeeperAdapter(List<Tmall_shopkeeper_hostGoodsBean.DataBean.ListBean> goodsBeanlist, android.content.Context context) {
        this.goodsBeanlist = goodsBeanlist;
        this.context = context;

            arrayList = new ArrayList<String>();
            isChecked = new ArrayList<>();
            for (int i = 0; i < length; i++) {
                isChecked.add(false);
            }
            numbers = new ArrayList<>();
            for (int i = 0; i < length; i++) {
                numbers.add("");
            }
            checkedIndex = new ArrayList<>();
            checkBox = new ArrayList<>();

    }

    public List<Tmall_shopkeeper_hostGoodsBean.DataBean.ListBean> getGoodsBeanlist() {
        return goodsBeanlist;
    }

    public void setGoodsBeanlist(List<Tmall_shopkeeper_hostGoodsBean.DataBean.ListBean> goodsBeanlist) {
        this.goodsBeanlist = goodsBeanlist;
    }

    @Override
    public ShopkeeperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View holder = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_specail_gridview, parent, false);
        holder.setOnClickListener(this);

            return new ShopkeeperViewHolder(holder);

    }

    @Override
    public void onBindViewHolder(final ShopkeeperViewHolder holder, final int position) {

            holder.itme_shopkeeper_goodsName.setText("             "+goodsBeanlist.get(position).getGoodsName());
            Glide.with(context).load(goodsBeanlist.get(position).getMainImageUrl()).into(holder.imageView4);
            holder.itme_tmall_price.setText("¥"+goodsBeanlist.get(position).getPrice()+"");
            holder.itme_tmall_discountprice.setText(""+goodsBeanlist.get(position).getConPrice()+"");
            holder.itme_tmall_couponMoney.setText("降"+goodsBeanlist.get(position).getCouponMoney()+"元");
            holder.itme_tmall_totalSales.setText("已抢"+goodsBeanlist.get(position).getTotalSales()+"件");
//            holder.itme_tmall_wlCommission.setText("分享赚 ¥"+goodsBeanlist.get(position).getWlCommission()+"");
        holder.itemView.setTag(position);
        //解决RecyclerView和checkBox问题
        holder.cb_checked_share_specila.setTag(position);
        if (Tmall_shopkeeperFragment.hmap.containsKey(position)) {
            holder.cb_checked_share_specila.setChecked(Tmall_shopkeeperFragment.hmap.get(position));
        } else {
            holder.cb_checked_share_specila.setChecked(false);//true or false 都可以，看实际需求
        }
        if(SPUtils.getInstance().getString(SPConstants.KEY_ISSTORE).equals("1")){
            holder.cb_checked_share_specila.setVisibility(View.VISIBLE);
            holder.tv_lijishare_btn_specila_pt.setVisibility(View.GONE);
            holder.tv_lijishare_btn_specila.setVisibility(View.VISIBLE);
        }else{
            holder.cb_checked_share_specila.setVisibility(View.GONE);
            holder.tv_lijishare_btn_specila_pt.setVisibility(View.VISIBLE);
            holder.tv_lijishare_btn_specila.setVisibility(View.GONE);
        }
        if( goodsBeanlist.get(position).getWlCommission()==0|| goodsBeanlist.get(position).getWlCommission()==0.0|| goodsBeanlist.get(position).getWlCommission()==0.00){
            holder.tv_lijishare_btn_specila.setText("立即分享" );
            holder.tv_lijishare_btn_no_specila.setText("立即分享" );
        }else {
            holder.tv_lijishare_btn_specila.setText("分享赚 ¥" + goodsBeanlist.get(position).getWlCommission());
            holder.tv_lijishare_btn_no_specila.setText("分享赚 ¥" + goodsBeanlist.get(position).getWlCommission());
        }
        holder.tv_lijishare_btn_specila.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ShopYouhuiShareActivity.class);
                intent.putExtra("id", goodsBeanlist.get(position).getId());
                context.startActivity(intent);
                holder.tv_lijishare_btn_no_specila.setVisibility(View.VISIBLE);
                holder.tv_lijishare_btn_specila.setVisibility(View.GONE);

            }
        });
        if(goodsBeanlist.get(position).getSpuPlatform().equals("jd")){
            holder.iv_speclia_jdOryh.setImageResource(R.drawable.jingdong);
        }else if(goodsBeanlist.get(position).getSpuPlatform().equals("yh")){
            holder.iv_speclia_jdOryh.setImageResource(R.drawable.youhui);
        }
try {
    //  不要动
    holder.cb_checked_share_specila.setChecked(isChecked.get(position));
    holder.cb_checked_share_specila.setText(numbers.get(position));
    holder.cb_checked_share_specila.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean check;
            if (checkedNumber < maxChecked) {
                if (!isChecked.get(position)) {
                    checkedIndex.add(position);
                    checkBox.add(holder.cb_checked_share_specila);
                    checkedNumber++;
                    setTexta();
                } else {
                    clearText(position, holder.cb_checked_share_specila);
                }
                check = !isChecked.get(position);
            } else {
                if (checkedNumber == maxChecked && isChecked.get(position)) {
                    clearText(position, holder.cb_checked_share_specila);
                } else {
                    Toast.makeText(context, "最多只能选" + maxChecked + "个", Toast.LENGTH_SHORT).show();
                }
                check = false;
            }
            isChecked.set(position, check);
            holder.cb_checked_share_specila.setChecked(isChecked.get(position));
        }
    });

    Contig.checkedIndexkeep = checkedIndex;

}catch (Exception e){

}

        holder.tv_lijishare_btn_no_specila.setVisibility(View.GONE);
//        holder.tv_lijishare_btn.setVisibility(View.VISIBLE);
        for (int s = 0; s < Contig.ShareList.size(); s++) {
            if (goodsBeanlist.get(position).getId().equals(Contig.ShareList.get(s))) {
                holder.tv_lijishare_btn_no_specila.setVisibility(View.VISIBLE);
                holder.tv_lijishare_btn_specila.setVisibility(View.GONE);
                Log.e("5647","youbianhua          "+goodsBeanlist.get(position).getId());


            }
        }


    }

    @Override
    public int getItemCount() {

        return goodsBeanlist.size();
    }
    public void clearText(int position, CheckBox cb_checked) {
        for (int i = 0; i < checkedIndex.size(); i++) {
            if (checkedIndex.get(i) == position) {
                numbers.set(position, "");
                checkedIndex.remove(i);
                checkBox.remove(i);
                break;
            }
        }
        checkedNumber--;
        setTexta();
        numbers.set(position, "");
        cb_checked.setText("");
    }

    public void setTexta() {
        for (int i = 0; i < checkedIndex.size(); i++) {
            numbers.set(checkedIndex.get(i), (i + 1) + "");
            checkBox.get(i).setText(numbers.get(checkedIndex.get(i)));

        }
    }

    public interface MyShopkeeperItemclickListener {
        void Shopkeeperitemclick(View view, int position);
    }
    public MyShopkeeperItemclickListener mMyShopkeeperItemclickListener;

    public void setmMyItemclickListener(MyShopkeeperItemclickListener mMyShopkeeperItemclickListener) {
        this.mMyShopkeeperItemclickListener = mMyShopkeeperItemclickListener;
    }
    @Override
    public void onClick(View v) {
        if (mMyShopkeeperItemclickListener != null) {
            mMyShopkeeperItemclickListener.Shopkeeperitemclick(v, (Integer) v.getTag());
        }
    }

    class ShopkeeperViewHolder extends RecyclerView.ViewHolder {

        private final TextView itme_shopkeeper_goodsName;
        private final ImageView imageView4;
        private final TextView itme_tmall_price;
        private final TextView itme_tmall_discountprice;
        private final TextView itme_tmall_couponMoney;
        private final TextView itme_tmall_totalSales;
//        private final TextView itme_tmall_wlCommission;
        private final CheckBox cb_checked_share_specila;
        private final TextView tv_lijishare_btn_no_specila;
        private final TextView tv_lijishare_btn_specila;
        private final TextView tv_lijishare_btn_specila_pt;
        private final ImageView iv_speclia_jdOryh;

        public ShopkeeperViewHolder(View itemView) {
            super(itemView);
            itme_shopkeeper_goodsName= (TextView) itemView.findViewById(R.id.tv_share_today_name_specila);
            imageView4 = (ImageView) itemView.findViewById(R.id.iv_item_share_todaySeletion_shopimage_specila);
            itme_tmall_price = (TextView) itemView.findViewById(R.id.tv_share_today_yuanprice_specila);
            itme_tmall_discountprice = (TextView) itemView.findViewById(R.id.tv_share_xianprice_specila);
            itme_tmall_couponMoney = (TextView) itemView.findViewById(R.id.tv_share_taday_redprice_spec);
            itme_tmall_totalSales = (TextView) itemView.findViewById(R.id.tv_share_todaySeletion_yiqiangnum_specila);
//            itme_tmall_wlCommission = (TextView) itemView.findViewById(R.id.tv_lijishare_btn_specila);
            cb_checked_share_specila = (CheckBox) itemView.findViewById(R.id.cb_checked_share_specila);
            tv_lijishare_btn_no_specila= (TextView) itemView.findViewById(R.id.tv_lijishare_btn_no_specila);
            tv_lijishare_btn_specila= (TextView) itemView.findViewById(R.id.tv_lijishare_btn_specila);
            tv_lijishare_btn_specila_pt= (TextView) itemView.findViewById(R.id.tv_lijishare_btn_specila_pt);
            iv_speclia_jdOryh= (ImageView) itemView.findViewById(R.id.iv_speclia_jdOryh);
        }
    }
}
